package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.glowdayz.glowmee.dao.AdminTableDAO;

public class ListEtcBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListEtcBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		try {
			ArrayList<AdminTableCMD> list = AdminTableDAO.getInstance().getAdminTableList();
			
			for (int i=0; i<list.size(); i++) {
				AdminTableCMD cmd = list.get(i);
				if (cmd.getBoardType().equals("FACEBOOK")) {
					request.setAttribute("facebook", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("PARTNER")) {
					request.setAttribute("partner", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("INTROIMAGE")) {
					request.setAttribute("introimage", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("IPHONE")) {
					request.setAttribute("iphone", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("ANDROID")) {
					request.setAttribute("android", cmd.getBoardText());
				}
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listEtc.jsp");
		dpr.forward(request, response);
	}

	/** 기타정보를 수정할때 실행되는 메서드 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String facebookLink = request.getParameter("facebookLink");
		String partnerLink = request.getParameter("partnerLink");
		String glowmeeIntroLink = request.getParameter("glowmeeIntroLink");
		String iosVersion = request.getParameter("iosVersion");
		String androidVersion = request.getParameter("androidVersion");
		
		ArrayList<AdminTableCMD> cmdList = new ArrayList<AdminTableCMD>();
		cmdList.add(new AdminTableCMD("FACEBOOK", facebookLink));
		cmdList.add(new AdminTableCMD("PARTNER", partnerLink));
		cmdList.add(new AdminTableCMD("INTROIMAGE", glowmeeIntroLink));
		cmdList.add(new AdminTableCMD("IPHONE", iosVersion));
		cmdList.add(new AdminTableCMD("ANDROID", androidVersion));
		
		try {
			int insert = AdminTableDAO.getInstance().updateAdminTable(cmdList);
			if (insert > 0) {
				request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		try {
			ArrayList<AdminTableCMD> list = AdminTableDAO.getInstance().getAdminTableList();
			
			for (int i=0; i<list.size(); i++) {
				AdminTableCMD cmd = list.get(i);
				if (cmd.getBoardType().equals("FACEBOOK")) {
					request.setAttribute("facebook", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("PARTNER")) {
					request.setAttribute("partner", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("INTROIMAGE")) {
					request.setAttribute("introimage", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("IPHONE")) {
					request.setAttribute("iphone", cmd.getBoardText());
				}
				if (cmd.getBoardType().equals("ANDROID")) {
					request.setAttribute("android", cmd.getBoardText());
				}
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listEtc.jsp");
		dpr.forward(request, response);
	}

}